﻿using Dapper;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using YunNanHealthProject_Register.Entity;

namespace YunNanHealthProject_Register.Model
{

    class BasicInfo
    {

        private static string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;

        IDbConnection connection = new SqlConnection(connStr);

        //获取医院基本信息
        public HospitalInfo GetHosPitalInfo()
        {
            string sql = @"SELECT '43120477700' AS hosOrgCode, 
                                  '43120477700' AS orgCode,     
                                  RTRIM(UNITNAME) AS hosName,   
                                  '3' AS hospitalGrade,         
                                  '1' AS hospitalLevel,         
                                  '1' AS hospitalType,          
                                  '3' AS orgSort,               
                                  UnitPhoto AS hospitalImage,   
                                  UnitAreaCode AS HospitalArea, 
                                  UnitTel AS hospitalTel        
                           FROM dbo.unitset (NOLOCK) ";
            SqlParameter[] parameters = new SqlParameter[]
          {
          };

            return connection.Query<HospitalInfo>(sql).First();
        }

        //1级科室信息
        public List<KSCodeLevel1> GetKSCode()
        {
            string sql = @" SELECT '43120477700' AS hosOrgCode,        
                                   RTRIM(LBCODE) AS deptCode,          
                                   RTRIM(LBName) AS deptName,          
                                   CASE LBName WHEN '非医疗系列' THEN '管理科室' 
                                   ELSE LBName END AS normdeptName,             
                                   RTRIM(LBCODE) AS normdeptCode,      
                                   RTRIM(LBNAME) AS normdeptName,      
                                   '' AS deptDesc,              
                                   '0' AS isRegister,           
                                   '1' AS isDelete,             
                                   '' AS indexNo               
                            FROM dbo.KSCODE(NOLOCK)             
                            GROUP BY LBCODE,LBNAME ";

            return connection.Query<KSCodeLevel1>(sql).ToList();
        }

        //2级科室信息
        public List<KSCodeLevel2> GetKSCode2()
        {
            string sql = @" SELECT '43120477700' AS hosOrgCode, 
                                   RTRIM(A.LBCODE) AS oneDeptCode,       
                                   RTRIM(A.CODE) AS deptCode,            
                                   RTRIM(A.NAME) AS deptName,            
                                   '' AS deptDesc,              
                                   '0' AS isRegister,           
                                   '1' AS isDelete,             
                                   '' AS indexNo                
                            FROM dbo.KSCODE A(NOLOCK) JOIN MZSchedule B(NOLOCK) ON A.Code=B.KSCode 
                            GROUP BY A.Code,A.Name,A.LBCODE     ";
            return connection.Query<KSCodeLevel2>(sql).ToList();
        }

        //医师信息
        public List<YSCode> GetYSCodes()
        {
            string sql = @"SELECT DISTINCT '43120477700' AS hosOrgCode,             
                                  SUBSTRING(A.KSCODE,1,2) AS oneDeptCode,    
                                  RTRIM(A.KSCODE) AS deptCode,                       
                                  RTRIM(A.CODE) AS resourceCode,                     
                                  RTRIM(A.NAME) AS resourceName,                     
                                  CASE A.YHFLAG WHEN 2 THEN '234'          
                                              WHEN 3 THEN '231'            
                                              WHEN 5 THEN '233'            
                                              WHEN 6 THEN '232'            
                                  ELSE '6' END AS doctTile,                
                                  '1' AS personType,                       
                                  '暂无' AS personId,                      
                                  '' AS doctImg,                          
                                  '9' AS doctSex,                         
                                  '暂无' AS doctInfo,                      
                                  '暂无' AS doctSpecialty,                 
                                  '0' AS isRegister,                      
                                  '1' AS isDelete,                        
                                  A.CODE AS listOrder,                     
                                  '1970-01-01' AS doctTileDate,            
                                  A.CertificateNumber AS qualificationNum, 
                                  A.Tel AS tel,                            
                                  CASE A.YHFLAG WHEN 2 THEN '4'            
                                              WHEN 3 THEN '1'              
                                              WHEN 5 THEN '3'              
                                              WHEN 6 THEN '2'              
                                  ELSE '6' END AS dutyRank,                
                                  '暂无' AS doctAdd                        
                           FROM dbo.YSCODE A(NOLOCK) JOIN MZSchedule B(NOLOCK) ON A.Code=B.YSCode
                           WHERE YHFLAG IN(2,3,5,6) ";

            return connection.Query<YSCode>(sql).ToList();
        }

        //门诊科室信息
        public List<OutPatientKSInfo> GetOutPatientKSInfos()
        {
            string sql = @" SELECT DISTINCT '43120477700' AS hosOrgCode, 
                                   RTRIM(A.LBCODE) AS oneDeptCode,       
                                   RTRIM(A.CODE) AS deptCode,            
                                   RTRIM(A.CODE) AS resourceCode,             
                                   RTRIM(A.NAME) AS resourceName,            
                                   RTRIM(A.LBNAME) AS outPatInfo,             
                                   RTRIM(KSAddress) AS doctAdd,               
                                   RTRIM(CODE) AS indexNo,                   
                                   CASE WHEN A.StopDate IS NULL THEN '1' ELSE '0' END AS isDelete                     
                            FROM dbo.KSCODE A(NOLOCK) JOIN MZSchedule B(NOLOCK) ON A.Code=B.KSCode";
            return connection.Query<OutPatientKSInfo>(sql).ToList();

        }
    }
}
